package ghostcat.util.core
{
    import flash.utils.*;
    import ghostcat.events.*;
    import ghostcat.util.*;

    public class UniqueCall extends Object
    {
        protected var para:Array;
        public var handler:Function;
        public var frame:Boolean = false;
        public var inv:int;
        public var dirty:Boolean = false;
        private var tick:Tick;

        public function UniqueCall(param1:Function, param2:Boolean = false, param3:Number = NaN)
        {
            tick = Tick.instance;
            this.handler = param1;
            this.frame = param2;
            this.inv = param3;
            return;
        }// end function

        public function vaildNow() : void
        {
            if (para)
            {
                handler.apply(null, para);
            }
            else
            {
                handler();
            }
            dirty = false;
            para = null;
            return;
        }// end function

        public function invalidate(... args) : void
        {
            if (dirty)
            {
                return;
            }
            dirty = true;
            this.para = args;
            if (frame)
            {
                tick.addEventListener(TickEvent.TICK, tickHandler);
            }
            else
            {
                setTimeout(vaildNow, inv);
            }
            return;
        }// end function

        private function tickHandler(event:TickEvent) : void
        {
            tick.removeEventListener(TickEvent.TICK, tickHandler);
            vaildNow();
            return;
        }// end function

    }
}
